package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.SysFileRsp;
import com.ruoyi.system.domain.SystemFile;
import com.ruoyi.system.service.ISysFileService;
import com.ruoyi.system.utils.ParametricTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPhoneRsp;
import com.ruoyi.system.service.ISysPhoneRspService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中移接口返回Controller
 * 
 * @author ruoyi
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/system/prsp")
public class SysPhoneRspController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(SysPhoneRspController.class);

    @Autowired
    private ISysPhoneRspService sysPhoneRspService;

    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询中移接口返回列表
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPhoneRsp sysPhoneRsp)
    {
        startPage();
        List<SysPhoneRsp> list = sysPhoneRspService.selectSysPhoneRspList(sysPhoneRsp);
        return getDataTable(list);
    }

    /**
     * 导出中移接口返回列表
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:export')")
    @Log(title = "中移接口返回", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPhoneRsp sysPhoneRsp)
    {
        List<SysPhoneRsp> list = sysPhoneRspService.selectSysPhoneRspList(sysPhoneRsp);
        ExcelUtil<SysPhoneRsp> util = new ExcelUtil<SysPhoneRsp>(SysPhoneRsp.class);
        util.exportExcel(response, list, "中移接口返回数据");
    }

    @Log(title = "中移接口返回", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        // 上传并返回新文件名称
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
        SystemFile systemFile = new SystemFile();
        systemFile.setFilePath(filePath);
        systemFile.setFileName(file.getOriginalFilename());
        ExcelUtil<SysPhoneRsp> util = new ExcelUtil<>(SysPhoneRsp.class);
        List<SysPhoneRsp> sfs = util.importExcel(file.getInputStream());
        systemFile.setNum(sfs.size());
        int i = sysFileService.insertSysFile(systemFile);
        Long fileId = systemFile.getId();
        log.info("SysFileRsp:{}", sfs);
        String message = sysPhoneRspService.importPhone(sfs, fileId);

        return success(message);
    }

    /**
     * 获取中移接口返回详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:query')")
    @GetMapping(value = "/{empowerNo}")
    public AjaxResult getInfo(@PathVariable("empowerNo") String empowerNo)
    {
        return success(sysPhoneRspService.selectSysPhoneRspByEmpowerNo(empowerNo));
    }

    /**
     * 获取中移接口返回详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:query')")
    @GetMapping(value = "/updatePhoneRsp")
    public AjaxResult updatePhoneRsp(@RequestParam("destDir") String destDir)
    {
        ParametricTask.updatePhoneRsp("+ZOiUjTlU+zwsYfVRq2O8m8xHrRbtUVSgVXPZoIPxgg=", destDir);
        return success();
    }

    /**
     * 获取中移接口返回详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:query')")
    @GetMapping(value = "/uploadData")
    public AjaxResult uploadData(@RequestParam("fileId") Long fileId)
    {
        return success(sysPhoneRspService.uploadData(fileId));
    }
    /**
     * 获取中移接口返回详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:query')")
    @GetMapping(value = "/getTaskStatus")
    public AjaxResult uploadData(@RequestParam("requestId") String requestId,@RequestParam("fileId") Long fileId)
    {
        return success(sysPhoneRspService.getTaskStatus(requestId, fileId));
    }

    /**
     * 新增中移接口返回
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:add')")
    @Log(title = "中移接口返回", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPhoneRsp sysPhoneRsp)
    {
        return toAjax(sysPhoneRspService.insertSysPhoneRsp(sysPhoneRsp));
    }

    /**
     * 修改中移接口返回
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:edit')")
    @Log(title = "中移接口返回", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPhoneRsp sysPhoneRsp)
    {
        return toAjax(sysPhoneRspService.updateSysPhoneRsp(sysPhoneRsp));
    }

    /**
     * 删除中移接口返回
     */
    @PreAuthorize("@ss.hasPermi('system:prsp:remove')")
    @Log(title = "中移接口返回", businessType = BusinessType.DELETE)
	@DeleteMapping("/{empowerNos}")
    public AjaxResult remove(@PathVariable String[] empowerNos)
    {
        return toAjax(sysPhoneRspService.deleteSysPhoneRspByEmpowerNos(empowerNos));
    }
}
